<?php
class DefaultCmsController extends FrontController
{
//    public $isFlashDataPage = false;
    public $allowEdit = false;
    
    public function init()
    {
        if (!Yii::app()->user->checkAccess('administrator'))
        {
            $criteria = new CDbCriteria;
            $criteria->compare('site_id', Yii::app()->cmsManager->site['id']);
            $criteria->compare('type', Workflow::TYPE_FINAL);
            $model = Workflow::model()->find($criteria);
            if (is_object($model))
            {
                $url = $model->url;
                if (strpos($url, 'http://') === false)
                    $url = 'http://'.$url;
                $this->redirect($url);
            }
            else
                Yii::app()->end();
        }
        
        // for the flash cookie which is set by JS, we must get the cookie 
        // before Yii's cookie validation is turned on
        if (!isset(Yii::app()->request->Cookies['bflash']) || Yii::app()->request->Cookies['bflash']->value == false) 
            Yii::app()->user->setState('HAS_FLASH',0);
        else 
            Yii::app()->user->setState('HAS_FLASH',1);

        Yii::app()->sourceLanguage = 'en_us';
        Yii::app()->language = Yii::app()->cmsManager->site['locale_id'];
        
        parent::init();
        
        // turn security on
        Yii::app()->request->enableCsrfValidation = true;
        Yii::app()->request->enableCookieValidation = true;
    }
    
    public function getGuestAllowedActions(){
        return array('index');
    }
        
    public function actionIndex()
    {
        $this->allowEdit = $this->get('allowEdit',0);
        Yii::app()->theme = Yii::app()->cmsManager->CmsTheme;
        
        if (Yii::app()->request->getRequestUri() == '/')
        {
            if ($this->isBrowserSupported() == false){
                // not supported user agent, send the to homepage
                $this->redirect(baseUrl().'/index.html');
            }
            $this->render('index');
            return;
        }
        
        //register favorite icon
        $favIcon = Yii::app()->cmsManager->site['fav_icon'];
        if (!empty($favIcon))
            Yii::app()->clientScript->registerLinkTag('icon', 'image/png', $favIcon);
        
        $reuqestedPageUrl = Yii::app()->urlManager->RequestedPageUrl;
        $html = FSM::run('Cms.Page.renderPage', array('url' => $reuqestedPageUrl))->html;
        $html = str_replace(array('<!DOCTYPE html>'), '', $html);
    //        $html = $html->saveHTML();
            // strip <root> tag
    //        $html = substr($html,6,strlen($html)-14);
            
        if ($this->get('content-type') == 'xml')
            header('content-type:text/xml');
        // add reference for http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
        if ($this->allowEdit)
            echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';

        echo $html;
    }
    
    protected function isBrowserSupported(){
        $agents = array(
            array('Mozila','Gecko/','Firefox/'),                // Firefox
            '(KHTML, like Gecko) Chrome',                       // Google Chrome
            'Opera',                                            // Opera
            'MSIE 10.0','MSIE 9.0','MSIE 8.0','MSIE 7.0',       // MS IE 7+
            array('AppleWebKit','(KHTML, like Gecko)'),         // Safari
            array('Konqueror','(like Gecko)'),                  // Konqueror
        );
        
        $ua = Yii::app()->getRequest()->getUserAgent();
        foreach($agents as $keys)
        {
            if (is_string($keys))
            {
                if (strpos($ua, $keys) !== false) return true;
            }
            else
            {   
                $supported = true;
                foreach($keys as $key)
                    if (strpos($ua, $key) !== false) $supported = false;

                if ($supported) return true;
            }
        }
        
        return false;
    }
}